package com.cheyun.netsalev3.view.defeataudit

import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager.widget.ViewPager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.databinding.ActivityDefeatAuditBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.DefeatAuditActivityViewMoldel

class DefeatAuditActivity : BaseActivity<ActivityDefeatAuditBinding>() {

    override fun initContentView(): Int {
        return R.layout.activity_defeat_audit
    }

    private var titleList = arrayListOf("战败审核", "无效审核")
    var nkeys = arrayOf("archivecheck.fail", "archivecheck.invalid")
    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getDefeatAuditActivityViewMoldelFactory(nkeys)
        return ViewModelProviders.of(this, factory).get(DefeatAuditActivityViewMoldel::class.java)
    }


    override fun initView() {
        var list = ArrayList<Fragment>()
        list.add(DefeatAuditActivityFragment.newInstance(nkeys[0]))
        list.add(DefeatAuditActivityFragment.newInstance(nkeys[1]))
        var adapter = HomePageAdapter(supportFragmentManager, list)
        adapter.titles = titleList
        binding.viewPager.adapter = adapter
        binding.tabLayout.setSelectedTabIndicator(R.drawable.tab_indicator)
        binding.tabLayout.setupWithViewPager(binding.viewPager)

        binding.leftBut.setOnClickListener {
            finish()
        }
        binding.viewModel?.setPosition(nkeys[0])
        //设置滑动监听器
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                binding.viewModel?.setPosition(nkeys[position])
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //1确认战败返回  虚假战败返回  2确认无效 虚假无效返回
        when(resultCode){
            1->binding.viewModel?.refresh(nkeys[0])
            2->binding.viewModel?.refresh(nkeys[1])
        }
    }

    override fun initData() {
    }

}
